import { useState, useReducer } from 'react';

import useRequest from "hook/useRequest"
import service from 'service';
/**

页面初始状态: 显示加载动画,当出现请求异常时会显示错误提示
下拉刷新状态: 不显示加载动画, 使用默认加载动画
加载更多加载: 不显示加载动画, 使用默认加载动画

正常加载状态
刷新加载状态

*/
const defaultState = { type: 'initialize', page: 1, pageSize: 10, lists: [], total: 0 };

// function reducer(state, action) {
//     switch (action.type) {
//         case 'initialize':
//             return { page: 1, ...state };
//         case 'nextPage':
//             return { page: state.page + 1, ...state };
//         case 'refresh':
//             return { page: 1, ...state };
//         default:
//             throw new Error();
//     }
// }

function reducer(state, action) {
    switch (action.type) {
        case 'initialize':
            return { ...state, type: action.type, page: 1, };
        case 'loadMore':
            return { ...state, type: action.type, page: state.page + 1, };
        case 'refresh':
            return { ...state, type: action.type, page: 1, };
        case 'data':
            return { ...state, ...action.payload, };
        default:
            throw new Error();
    }
}

/**
 * @name 移动端无限加载列表
 */
export default function useList(fetch, initialState) {
    const [state, dispatch] = useReducer(reducer, initialState, function (data) {
        if (data) {
            return { ...defaultState, ...data }
        }
        return defaultState
    });
    const requestApi = useRequest(fetch)
    const {
        type,
        page, // 页码
        pageSize, // 每页条数
        lists,
        total,
    } = state;
    const {
        status, // 请求状态
        statusText, // 请求状态描述
        loading, // 加载状态
        request, // 请求方法
        cancel, // 取消请求方法
    } = requestApi;
    const initialize = (params) => {
        dispatch({ type: 'initialize' })
        return request({ ...state, ...params }).then(res => {
            dispatch({
                type: 'data', payload: {
                    lists: res?.list || [],
                    total: res?.total || 0
                }
            })
        })
    }
    const loadMore = (params) => {
        dispatch({ type: 'loadMore' })
        return request({ ...state, ...params }).then(res => {
            dispatch({
                type: 'data', payload: {
                    lists: state.lists.concat(res?.list || []),
                    total: res?.total || 0
                }
            })
        })
    }
    const refresh = (params) => {
        dispatch({ type: 'refresh' })
        return request({ ...state, ...params }).then(res => {
            dispatch({
                type: 'data', payload: {
                    lists: res?.list || [],
                    total: res?.total || 0
                }
            })
        })
    }

    return {
        type,
        page,
        pageSize,
        lists,
        total,
        // 
        totalPage: Math.ceil(total / pageSize), // 总条数

        status,
        statusText,
        loading,
        request,
        cancel,

        initialize,
        loadMore,
        refresh,
    }
}